{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "12f53663-18e0-4799-8a58-eb56e1a3927c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import subprocess as sp\n",
    "import cv2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8b9f9eff-e46b-4c78-948b-0a2ce1f2373d",
   "metadata": {},
   "outputs": [],
   "source": [
    "rtmp_url = \"rtmp://localhost:1935/{}/{}\".format(\"live\", \"rfBd56ti2SMtYvSgD5xAV0YU99zampta7Z7S575KLkIZ9PYk\")\n",
    "camera_path = 0\n",
    "cap = cv2.VideoCapture(camera_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "75a583e7-e3e2-4f55-a276-e1fd859f866b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get video information\n",
    "fps = int(cap.get(cv2.CAP_PROP_FPS))\n",
    "width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))\n",
    "height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4539cb7c-0af1-4163-989d-040c4dfa4990",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ffmpeg command\n",
    "command = ['ffmpeg',\n",
    "           '-y',\n",
    "           '-f', 'rawvideo',\n",
    "           '-vcodec', 'rawvideo',\n",
    "           '-pix_fmt', 'bgr24',\n",
    "           '-s', \"{}x{}\".format(width, height),\n",
    "           '-r', str(fps),\n",
    "           '-i', '-',\n",
    "           '-c:v', 'libx264',\n",
    "           '-pix_fmt', 'yuv420p',\n",
    "           '-preset', 'ultrafast',\n",
    "           '-f', 'flv',\n",
    "           rtmp_url]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7a221c1f-181f-42f4-b95e-81ce30b477e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 管道配置\n",
    "p = sp.Popen(command, stdin=sp.PIPE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "16f615fd-f36a-4007-b0a6-1ad83444672e",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_24772/3796088485.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# read webcamera\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;32mwhile\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcap\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0misOpened\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m     \u001b[0mret\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mframe\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcap\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mread\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mret\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m         \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Opening camera is failed\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "# read webcamera\n",
    "while(cap.isOpened()):\n",
    "    ret, frame = cap.read()\n",
    "    if not ret:\n",
    "        print(\"Opening camera is failed\")\n",
    "        break\n",
    "\n",
    "    # process frame\n",
    "    # your code\n",
    "    # process frame\n",
    "\n",
    "    # write to pipe\n",
    "    p.stdin.write(frame.tobytes())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7eef73c2-f776-4c9e-95ec-934d7ef08636",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(480, 640, 3)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame.shape\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ccaaa5b-a3e8-4df1-a95d-65dac6816ca9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
